<?php

namespace app\admin\controller\group;

use app\common\controller\Backend;

/**
 * 抱团
 *
 * @icon fa fa-circle-o
 */
class Group extends Backend
{

    /**
     * Group模型对象
     * @var \app\admin\model\group\Group
     */
    protected $model = null;
    protected $media_model = null;
    protected $apply_model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\group\Group;
        $this->media_model = new \app\admin\model\group\GroupMedia();
        $this->apply_model = new \app\admin\model\group\GroupProjectApply();
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                $row->visible(['group_id','user_id','nickname','name','people_num','realname','mobile','province','city','district','image','status','createtime']);
                
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }



    public function detail($ids = null)
    {
        $row = $this->model->get($ids);

        $this->view->assign("row", $row);

        $taboo_list = \app\admin\model\group\GroupTaboo::where('id','in',$row->taboo_id)->column('name');
        $this->view->assign("taboo_list", implode('/',$taboo_list));
        $user_level = explode(',',$row->user_level);
        $user_level_list = [];
        $level_list = userlevel();
        foreach ($level_list as $k=>$v){
            if (in_array($k,$user_level)){
                array_push($user_level_list,$v);
            }
        }

        $this->view->assign("user_level", implode('/',$user_level_list));
        $cert_list = $this->media_model->where(['group_id'=>$row->group_id,'type'=>0])->select();
        $image_list = $this->media_model->where(['group_id'=>$row->group_id,'type'=>1])->select();
        $video_list = $this->media_model->where(['group_id'=>$row->group_id,'type'=>2])->select();
        $this->view->assign("cert_list", $cert_list);
        $this->view->assign("image_list", $image_list);
        $this->view->assign("video_list", $video_list);
        $project_apply_list = $this->apply_model->where(['group_id'=>$row->group_id])->select();

        $this->view->assign("project_apply_list", $project_apply_list);
        return $this->view->fetch();
    }


    public function pass($ids = null)
    {
        $row = $this->model->get($ids);

        $row->status=1;
        $row->audit_time = time();
        if ($row->save()){
            $this->success('审核成功');
        } else {
            $this->error('审核失败');
        }
    }

    public function refuse($ids = null)
    {
        $row = $this->model->get($ids);
        $row->status = 2;
        $row->audit_time = time();
        if ($row->save()){

            $this->success('拒绝成功');
        } else {
            $this->error('拒绝失败');
        }
    }
}
